package com.zd.controller;

import com.zd.domain.Comment;
import com.zd.domain.Result;
import com.zd.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 查询评论
     * @param postId
     * @return
     */
    @GetMapping("findComments")
    public Result<List<Comment>> findComments(@RequestParam("postId") Integer postId){

        Result<List<Comment>> result = new Result<>();

        if (postId != 0){

            //根据postId查询评论
            List<Comment> comments = commentService.comments(postId);

            if (comments != null){
                result.setCode(200);
                result.setMsg("查询成功！");
                result.setData(comments);
            }else {
                result.setCode(400);
                result.setMsg("查询失败！");
            }

        }else {
            result.setCode(401);
            result.setMsg("客户端传输数据失败！");
        }

        return result;

    }

    @PostMapping("saveComment")
    public Result saveComment(@RequestBody Comment comment){

        Result result = new Result();

        int i = commentService.saveComment(comment);

        if ( i == 1){
            result.setCode(200);
            result.setMsg("发表评论成功！");

        }else {
            result.setCode(400);
            result.setMsg("发表失败！");
        }

        return result;


    }


}
